/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package updateGaji;

import Koneksi.KoneksiDatabase;
import UpdateKaryawan.updateKaryawanKontrol;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import Karyawan.Karyawan;
import hitunggaji.Gaji;

/**
 *
 * @author yolan
 */
public class updategajikontrol {

    private Connection conn;

    public updategajikontrol(Connection conn2) {
        this.conn = conn2;
    }

    public static updategajikontrol getKoneksi() throws SQLException {
        KoneksiDatabase konDB = new KoneksiDatabase();
        updategajikontrol upgajikontrol = new updategajikontrol(konDB.getConn());
        return upgajikontrol;
    }

    public List<Gaji> getAllgolongan() throws SQLException {
        PreparedStatement psmt = null;
        ResultSet rset = null;
        conn.setAutoCommit(false);
        String sql = "select golongan,gajigolongan from gaji order by gajigolongan";
        psmt = conn.prepareStatement(sql);
        rset = psmt.executeQuery();
        List<Gaji> st = new ArrayList<Gaji>();
        while (rset.next()) {
           Gaji stok = new Gaji();
            Karyawan k=new Karyawan();
            k.setGolongan(rset.getString(1));
            stok.setGolongan(k);  
           stok.setGajigolongan(rset.getDouble(2));
            st.add(stok);
        }
        conn.commit();
        return st;
    }

    public void updateDBPrepared(Gaji databarang) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "UPDATE gaji SET gajigolongan = ?"
                    + "WHERE golongan = ? ";

            statement = conn.prepareStatement(sql);
            statement.setDouble(1, databarang.getGajigolongan());
            statement.setString(2, databarang.getGolongan().getGolongan());
            
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException ex) {
                throw ex;
            }
        }
    }
       
    }

